package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsStorage;
import com.qf.service.StorageService;
import com.qf.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fy
 * @version V1.0
 * @Project workspace
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/6/21 10:50
 */
//解决前后端分离跨域访问问题
@CrossOrigin("*")
@RestController
@RequestMapping("/storage")
public class StorageController {

    @Autowired
    private StorageService storageService;
    /**
     * \查询对象数据列表
     *  http://localhost:8083/admin/storage/list?page=1&limit=20&sort=add_time&order=desc
     *  拼接参数
     *  http://localhost:8083/admin/storage/list?page=1&limit=20&
     *                                          key=123123&name=xxx%E5%9B%BE%E7%89%87&sort=add_time
     *                                          &order=desc
     *  springMvc注解@RequestParam 用于将请求参数区数据映射到功能处理方法的参数上
     *  也就是@RequestParam(defaultValue = 1) Integer page, 把1传给page
     * @return
     */
    /**
     * 查询对象数据列表
     * @param key 根据文件的key查找
     * @param name 根据文件名字查找
     * @param page 当前页，默认值1
     * @param limit  每一页显示条数，默认20条
     * @param sort 排序字段名，按照add_time添加时间排序
     * @param order 排序方式，默认降序desc
     * @return
     */
    @GetMapping("/list")
    public Object storage(String key, String name,
                          @RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer limit,
                          @RequestParam(defaultValue = "add_time") String sort,
                          @RequestParam(defaultValue = "desc") String order){

        IPage<DtsStorage> iPage = storageService.queryStoragePage(key, name, page, limit, sort, order);
       //获取结果集
        List<DtsStorage> list = iPage.getRecords();
        //查询总条数
        long total = iPage.getTotal();
        //封装对象返回
        Map<String,Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", list);
        return ResponseUtil.ok(data);
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/create")
    public Object add(@RequestParam("file") MultipartFile file) throws Exception{

        //1.获取文件名
        String fileName = file.getOriginalFilename();
        if (StringUtils.isEmpty(fileName)){
            throw  new RuntimeException("上传的文件为空不允许上传");
        }
        //2.获取文件拓展名
        String ext = fileName.substring(fileName.lastIndexOf("."));
        //3.获取文件大小
        long size = file.getSize();
        //4.获取文件内容
        byte[] content = file.getBytes();
        //5.上传并返回上传保存的文件对象
        DtsStorage storage = storageService.add(fileName, (int) size, ext, content);
        //6.封装数据返回
        return ResponseUtil.ok(storage);
    }

    /**
     *修改
     * @param storage
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody DtsStorage storage){
        int count = storageService.update(storage);
        if (count == 0){
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok();
    }

    /**
     * 删除
     * @param storage
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsStorage storage){
        String key = storage.getKey();

        if (StringUtils.isEmpty(key)){
            return ResponseUtil.badArgument();
        }

        storageService.deleteByKey(key);
        return ResponseUtil.ok();
    }
}
